package deployr.client.http;

import java.io.*;

public class Streams {

	public static String slurp(InputStream stream) {
		try {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] buff = new byte[4096];
			for (int len; (len = stream.read(buff)) != -1;) {
				outputStream.write(buff, 0, len);
			}
			return outputStream.toString();

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static final void copy(File file, OutputStream outputStream) {
		try {
			copy(inputStream(file), outputStream);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void copy(InputStream inputStream, OutputStream outputStream) {
		try {
			byte[] buf = new byte[1024];
			int len;
			while ((len = inputStream.read(buf)) > 0) {
				outputStream.write(buf, 0, len);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static final FileInputStream inputStream(File file) {
		try {
			return new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

}
